<?php
class Deal extends MY_Controller {
	
	var $currentPage = '';
	var $caption = 'Mc';
	
	public function view($dealid) {
		
		$data['page_was_found'] = false;
		$data['categorydeallist'] = $this->mDealCategory->getAllEntries();
		
		//related to deals
		$page = "dealpage";
		$data['page'] = $page;
		
		$individualdeal = $this->mDeal->viewDealDetails($dealid);
		if(count($individualdeal) > 0){
			$data['individualdeal'] = $individualdeal[0];
			$tenants = $this->mTenant->getEntryById($individualdeal[0]->tenant_id);
			if(count($tenants) > 0){
				$data['tenant'] = $tenants[0];
			}
		}
		
		if($this->session->userdata("client_id") == null){
			$page = "login";
			$data['page'] = $page;
		}
		
		if($page != ""){
			$pageData= $this->mPage->getEntryByName($page);
			if(count($pageData) == 0) 
				$data['page_was_found'] = false;
			else{
				$data['pageData'] = $pageData[0];
				$data['page_was_found'] = true;
			}
		}
		
		if($this->session->userdata('client_id')!=null ){
				$addressbook = $this->mAddressBook->find_by_user($this->session->userdata('client_id'));
				$data['addressbook'] = $addressbook[0];
		 }
		

		
		$this->loadClientPage($data);
	}
	
	
	
	function pay($dealid){
		
		$data['page_was_found'] = false;
		$data['categorydeallist'] = $this->mDealCategory->getAllEntries();
		
		$individualdeal = $this->mDeal->viewDealDetails($dealid);
		//related to deals
		$page = "paymentmessage";
		$data['page'] = $page;
		
	    $orderedDeal = $this->mOrderedDeal;
		$orderedDeal->modeofpayment = $this->input->post('modeofpayment',TRUE);
		$orderedDeal->sendasgift = $this->input->post('sendasgift',TRUE);
		$orderedDeal->client_id = $this->input->post('client_id',TRUE);
		$orderedDeal->receipient_fullname = $this->input->post('receipient_fullname',TRUE);
		$orderedDeal->receipient_address = $this->input->post('receipient_address',TRUE);
		$orderedDeal->receipient_notes = $this->input->post('receipient_notes',TRUE);
		$orderedDeal->tenant_id = $this->input->post('tenant_id',TRUE);
		$data['modeofpayment'] = $this->input->post('modeofpayment',TRUE);
		
		$orderedDeal->insert();
		
		$modeofpayment = $this->input->post('modeofpayment',TRUE);
		
		$this->loadClientPage($data);
		
	}
	
	function confirmcheckoutdetails($dealid){
		
		$data = $_POST;
		
		$data['page_was_found'] = false;
		$data['categorydeallist'] = $this->mDealCategory->getAllEntries();
		
		$individualdeal = $this->mDeal->viewDealDetails($dealid);
		if(count($individualdeal) > 0){
			$data['individualdeal'] = $individualdeal[0];
			$tenants = $this->mTenant->getEntryById($individualdeal[0]->tenant_id);
			if(count($tenants) > 0){
				$data['tenant'] = $tenants[0];
			}
			
			
		}
		
		if(isset($_POST['confirmDeal'])){
			$this->session->set_userdata('transaction_modeofpayment', $this->input->post('modeofpayment',TRUE));
			$this->session->set_userdata('transaction_sendasgift', $this->input->post('sendasgift',TRUE));
			$this->session->set_userdata('transaction_client_id', $this->input->post('client_id',TRUE));
			//data of user during transaction
			$this->session->set_userdata('transaction_client_fullname', $this->input->post('client_fullname',TRUE));
			$this->session->set_userdata('transaction_client_email', $this->input->post('client_email',TRUE));
			$this->session->set_userdata('transaction_client_address', $this->input->post('client_address',TRUE));
			
			$this->session->set_userdata('transaction_receipient_fullname', $this->input->post('receipient_fullname',TRUE));
			$this->session->set_userdata('transaction_receipient_email', $this->input->post('receipient_email',TRUE));
			$this->session->set_userdata('transaction_receipient_address', $this->input->post('receipient_address',TRUE));
			$this->session->set_userdata('transaction_notes', $this->input->post('transaction_notes',TRUE));
			$this->session->set_userdata('transaction_tenant_id', $this->input->post('tenant_id',TRUE));
			$this->session->set_userdata('transaction_total_amount', $individualdeal[0]->price);
		}
		
		$paymentMessage = $this->mPaymentMessages->getEntryById(1);//always get this number
		if(count($paymentMessage) > 0){
			$data['paymentMessage'] = $paymentMessage[0];
		}
				
		//related to deals
		$page = "checkoutdetails";
		$data['page'] = $page;
		
	     if($this->session->userdata('client_id')!=null ){
				$addressbook = $this->mAddressBook->find_by_user($this->session->userdata('client_id'));
				$data['addressbook'] = $addressbook[0];
		 }
		
		$this->loadClientPage($data);
		
	}
	
	function transactioncomplete($dealid){
		
		$data['page_was_found'] = false;
		$data['categorydeallist'] = $this->mDealCategory->getAllEntries();
		
		$individualdeal = $this->mDeal->viewDealDetails($dealid);
		//related to deals
		$page = "transactioncomplete";
		$data['page'] = $page;
		
		if(count($_POST) > 0){
			$orderedDeal = $this->mOrderedDeal;
			//data of user during transaction
			
			$orderedDeal->modeofpayment = $this->session->userdata('transaction_modeofpayment');
			$orderedDeal->sendasgift =  $this->session->userdata('transaction_sendasgift');
			$orderedDeal->client_id =  $this->session->userdata('client_id');
			$orderedDeal->receipient_fullname =  $this->session->userdata('transaction_receipient_fullname');
			$orderedDeal->receipient_address =  $this->session->userdata('transaction_receipient_address');
			$orderedDeal->receipient_notes =  $this->session->userdata('transaction_notes');
			$orderedDeal->receipient_email =  $this->session->userdata('transaction_receipient_email');
			$orderedDeal->deal_item_quantity =  1;
			
			//data of user during transaction
			$orderedDeal->client_fullname = $this->session->userdata('transaction_client_fullname');
			$orderedDeal->client_email = $this->session->userdata('transaction_client_email');
			$orderedDeal->client_address = $this->session->userdata('transaction_client_address');
			$orderedDeal->total_amount = $individualdeal[0]->price;
			$orderedDeal->deal_id = $individualdeal[0]->id;
			$orderedDeal->tenant_id = $individualdeal[0]->tenant_id;
			$orderedDeal->insert();
			
			$_orderedDeal = $orderedDeal->getEntryById($this->db->insert_id());
			$data['orderedDeal'] = $_orderedDeal[0];
			
		}
		
		
		
		$this->loadClientPage($data);
		
	}
	
	
	
}

?>